package cn.bjc.controller;

import cn.bjc.domain.Store;
import cn.bjc.service.IStoreService;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/store")
@RefreshScope // 动态刷新配置注解
public class StoreController {
    
    @Value("${test.name}")
    private String name;
    
    @Autowired
    private IStoreService storeService;
    
    
    @GetMapping("/getById/{id}")
    @SentinelResource(value = "store-service-getById",blockHandler = "BlockMethodsGetById")
    public Store getById(@PathVariable("id") Long id){
        System.out.println(name);
        return storeService.getById(id);
    }


    @GetMapping("/getByProductId/{product_id}")
    public Store getByProductId(@PathVariable("product_id") Long product_id){

        return storeService.getByProductId(product_id);
    }
    
    @PostMapping("/add")
    public Map<String,Object> add(@RequestBody Store store){
        
        return storeService.add(store);
    }
    
    @PostMapping("/update")
    public Map<String,Object> update(@RequestBody Store store){
        
        return storeService.update(store);
    }
    
    //限流的本类
    public Store BlockMethodsGetById(@PathVariable("id") Long id, BlockException e){
        e.printStackTrace();
        return new Store(-1L, null, null);
    }
    
}
